package service;

import beans.Shenqing;
import db.ShenqingDao;
import java.sql.SQLException;
import java.util.List;

public class ShenqingService {
    private ShenqingDao shenqingDao;

    public ShenqingService() {
        this.shenqingDao = new ShenqingDao();
    }

    // 新增方法：提交申请并返回订单号
    public int submitShenqing(Shenqing shenqing) throws SQLException {
        try {
            int orderId = shenqingDao.insertShenqing(shenqing);
            System.out.println("Service层提交申请成功，订单号: " + orderId);
            return orderId;
        } catch (SQLException e) {
            System.out.println("Service层提交申请出错: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    // 新增方法：提交申请并返回订单号（字符串类型）
    public String addShenqing(Shenqing shenqing) throws SQLException {
        try {
            int orderId = shenqingDao.insertShenqing(shenqing);
            System.out.println("Service层提交申请成功，订单号: " + orderId);
            return String.valueOf(orderId);
        } catch (SQLException e) {
            System.out.println("Service层提交申请出错: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    // 根据订单号获取申请
    public Shenqing getShenqingById(String orderId) {
        return shenqingDao.getShenqingById(orderId);
    }

    // 检查申请是否存在
    public boolean shenqingExists(String orderId) {
        return shenqingDao.shenqingExists(orderId);
    }

    // 新增方法：根据员工ID获取最新申请
    public Shenqing getLatestShenqingByEmployeeId(int employeeId) {
        return shenqingDao.getLatestShenqingByEmployeeId(employeeId);
    }

    public List<Shenqing> getAllShenqing() {
        try {
            List<Shenqing> list = shenqingDao.getAllShenqing();
            System.out.println("Service层获取所有申请: " + (list != null ? list.size() : 0) + " 条记录");
            return list;
        } catch (SQLException e) {
            System.out.println("Service层获取所有申请出错: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public List<Shenqing> getPendingShenqing() {
        List<Shenqing> list = shenqingDao.findByStatus("未审核");
        System.out.println("Service层获取待审核申请: " + (list != null ? list.size() : 0) + " 条记录");
        return list;
    }

    public List<Shenqing> getProcessedShenqing() {
        List<Shenqing> list = shenqingDao.findByStatus("未通过");
        System.out.println("Service层获取已处理申请: " + (list != null ? list.size() : 0) + " 条记录");
        return list;
    }

    public List<Shenqing> searchByOrderId(String orderId) {
        if (orderId != null && !orderId.isEmpty()) {
            List<Shenqing> list = shenqingDao.searchByOrderId(orderId);
            System.out.println("Service层按订单号搜索: " + (list != null ? list.size() : 0) + " 条记录");
            return list;
        }
        return getAllShenqing();
    }

    // 新增方法：更新申请状态
    public boolean updateShenqingStatus(Shenqing shenqing) {
        try {
            boolean success = shenqingDao.updateShenqingStatus(shenqing);
            if(success) {
                System.out.println("Service层更新申请状态成功，订单号: " + shenqing.getOrderId() + ", 新状态: " + shenqing.getStatus());
            } else {
                 System.out.println("Service层更新申请状态失败，订单号: " + shenqing.getOrderId() + ", 新状态: " + shenqing.getStatus());
            }
            return success;
        } catch (SQLException e) {
            System.out.println("Service层更新申请状态出错: " + e.getMessage());
            e.printStackTrace();
            //throw e; // 不再向上抛出SQLException，而是返回false
            return false;
        }
    }
}
